package com.ruoyi.web.controller.system;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.annotation.FastJsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.GPS;
import com.ruoyi.system.domain.TbDataReporting;
import com.ruoyi.system.domain.UserStaff;
import com.ruoyi.system.service.SysAccpetService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;
import sun.nio.cs.FastCharsetProvider;

import java.util.*;

@RestController
@RequestMapping("/car")
public class SysAccpet extends BaseController {

    private String prefix = "/car";

    @Autowired
    private Syspd syspd;

    @Autowired
    private SysAccpetService sysAccpetService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static Map<String,Integer> gpsToMqMap = new HashMap<>();

    @RequestMapping("/gpsToMqMapGet")
    public String gpsToMqMapGet(String vin){
        gpsToMqMap.put(vin,1);
        return vin;
    }
    @RequestMapping("/get")
    public String get(){
        return "success";
    }

    @RequestMapping("/gpsNotToMqMapGet")
    public String gpsNotToMqMapGet(String vin){
        gpsToMqMap.put(vin,0);
        return vin;
    }



    @RequestMapping("/accept")
    public void accept(@RequestBody GPS reporting){
//        System.out.println(jsonObject.toString());
            TbDataReporting reportings = new TbDataReporting();
            reportings.setSpeed(Double.parseDouble(reporting.getSpeed()));
             System.out.println(reportings.getSpeed());
             reportings.setVin(reporting.getVin());
             reportings.setIndex(0);
             reportings.setDirection("");
             reportings.setMileage(Double.parseDouble(reporting.getMileage()));
             reportings.setAccumulatedMileage(Double.parseDouble(reporting.getAccumulatedMileage()));
             reportings.setLifeMileage(Double.parseDouble(reporting.getLifeMileage()));
             reportings.setAltitude(Double.parseDouble(reporting.getAltitude()));
             reportings.setCurrent(Double.parseDouble(reporting.getCurrent()));
             reportings.setSoce(Double.parseDouble(reporting.getSoce()));
             reportings.setcTime(reporting.getCreateTime());
             reportings.setReportTime(reporting.getReportTime());
             reportings.setLatitude(Double.parseDouble(reporting.getLatitude()));
             reportings.setVoltage(Double.parseDouble(reporting.getVoltage()));
             reportings.setLongitude(Double.parseDouble(reporting.getLongitude()));

    //
//            int index = (int) jsonObject.get("index");
//        String ReportTime= (String) jsonObject.get("reportTime");
//            System.out.println(ReportTime);
//            String createTime = (String) jsonObject.get("CreateTime");
//            String vin = (String) jsonObject.get("Vin");
//        int Speed= (int) jsonObject.get("speed");
//            System.out.println(Speed);
//        Double mileage = (Double) jsonObject.get("Mileage");
//            Double Latitude = (Double) jsonObject.get("Latitude");
//            Double Longitude = (Double) jsonObject.get("Longitude");
//            Double AccumulatedMileage = (Double) jsonObject.get("AccumulatedMileage");
//            Double LifeMileage = (Double) jsonObject.get("LifeMileage");
//            Double Voltage = (Double) jsonObject.get("Voltage");
//            Double Current = (Double) jsonObject.get("Current");
//            Double Soce = (Double) jsonObject.get("Soce");
//            String direction = (String) jsonObject.get("direction");
//            reporting.setIndex(index);
//            reporting.setReportTime(ReportTime);
//            if (createTime!=null){
//                reporting.setCreateTime(createTime);
//            }else{
//                reporting.setCreateTime("");
//            }
//            if (vin!=null){
//                reporting.setVin(vin);
//            }else{
//                reporting.setVin(null);
//            }
//            if (Speed!=0){
//                reporting.setSpeed(Speed);
//            }else{
//                reporting.setSpeed(0);
//            }
//            if (mileage!=0){
//                reporting.setMileage(mileage);
//            }else{
//                reporting.setMileage(0);
//            }
//            if (Latitude!=null){
//                reporting.setLatitude(Latitude);
//            }else{
//                reporting.setLatitude(0);
//            }
//            if (Longitude!=null){
//                reporting.setLongitude(Longitude);
//            }else{
//                reporting.setLongitude(0);
//            }
//            if (AccumulatedMileage!=null){
//                reporting.setAccumulatedMileage(AccumulatedMileage);
//            }else{
//                reporting.setAccumulatedMileage(0);
//            }
//            if (LifeMileage!=null){
//                reporting.setLifeMileage(LifeMileage);
//            }else{
//                reporting.setLifeMileage(0);
//            }
//            if (Voltage!=null){
//                reporting.setVoltage(Voltage);
//            }else{
//                reporting.setVoltage(0);
//            }
//            if (Current!=null){
//                reporting.setCurrent(Current);
//            }else{
//                reporting.setCurrent(0);
//            }
//            if (Soce!=null){
//                reporting.setSoce(Soce);
//            }else{
//                reporting.setSoce(0);
//            }
//            if (Soce!=null){
//                reporting.setSoce(Soce);
//            }else{
//                reporting.setSoce(0);
//            }
//
//            if (direction!=null){
//                reporting.setDirection(direction);
//            }else{
//                reporting.setDirection(null);
//            }
        System.out.println(reportings.toString());
        if (gpsToMqMap.get(reportings.getVin()) != null){
            if (gpsToMqMap.get(reportings.getVin()) == 1){

                System.out.println("给rabbit发送信息");
                rabbitTemplate.convertAndSend("car",reporting);
            }else if (gpsToMqMap.get(reporting.getVin()) == 0){
                System.out.println("取消rabbit发送信息，清空rabbit消息");

                gpsToMqMap.put(reporting.getVin(),-1);
            }
        }else {
            sysAccpetService.insertAll(reportings);
        }


        }






}
